<template>
  <div class="app-container">
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="">
        <el-input
          clearable
          @clear="search()"
          v-model="pageDto.where.name"
          placeholder="请输入角色名称"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" type="primary" @click="search()"
          >查询</el-button
        >
      </el-form-item>
      <el-form-item>
        <el-button
          icon="el-icon-circle-plus-outline"
          type="primary"
          @click="addRole()"
          >添加</el-button
        >
      </el-form-item>
    </el-form>
    <el-table :data="roleList" stripe style="width: 100%">
      <el-table-column label="序号" type="index"></el-table-column>
      <el-table-column prop="name" label="角色名称"> </el-table-column>
      <el-table-column prop="createTime" label="创建时间"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            icon="el-icon-edit"
            @click="updateItem(scope.row)"
            type="primary"
            >修改</el-button
          >
          <el-button
            icon="el-icon-delete"
            @click="deleteItem(scope.row.id)"
            type="danger"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="float: right"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageDto.now"
      :page-sizes="[5, 15, 50, 100]"
      :page-size="pageDto.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <!-- 添加用户/修改 -->
    <el-dialog title="用户添加" :visible.sync="dialogFormVisible">
      <el-form :model="role">
        <el-form-item label="角色名称">
          <el-input v-model="role.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="分配权限">
          <el-select v-model="role.menuIds" multiple placeholder="请选择">
            <el-option
              v-for="item in menuList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="add()">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="用户修改" :visible.sync="dialogFormVisiblexg">
      <el-form :model="role">
        <el-form-item label="角色名称">
          <el-input v-model="role.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="重新分配权限">
          <el-select v-model="role.menuIds" multiple placeholder="请选择">
            <el-option
              v-for="item in menuList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisiblexg = false">取 消</el-button>
        <el-button type="primary" @click="add()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { page1, all, addRole, updateRole, deleteRole } from "@/api/role";
export default {
  data() {
    return {
      pageDto: {
        now: 1,
        size: 5,
        where: {
          name: "",
        },
      },
      role: { menuIds: [] },
      dialogFormVisible: false,
      dialogFormVisiblexg: false,
      total: 0,
      roleList: [],
      menuList: [],
    };
  },
  methods: {
    search() {
      page1(this.pageDto).then((res) => {
        this.roleList = res.list;
        this.total = res.total;
      });
    },
    handleSizeChange(size) {
      this.size = size;
      this.search();
    },
    handleCurrentChange(page) {
      this.pageDto.now = page;
      this.search();
    },
    addRole() {
      this.dialogFormVisible = true;
      this.role = {};
    },
    add() {
      if (this.role.id) {
        updateRole(this.role).then((res) => {
          this.dialogFormVisible = false;
          if (res.code == 200) {
            this.pageDto.now = 1;
            this.search();
          }
          this.$message({
            message: res.message,
            type: res.code == 200 ? "success" : "error",
          });
        });
      } else {
        addRole(this.role).then((res) => {
          this.dialogFormVisible = false;
          if (res.code == 200) {
            this.pageDto.now = 1;
            this.search();
          }
          this.$message({
            message: res.message,
            type: res.code == 200 ? "success" : "error",
          });
        });
      }
    },
    updateItem(row) {
      this.dialogFormVisiblexg = true;
      this.role = row;
    },
    allMenu() {
      all().then((res) => {
        this.menuList = res.data;
      });
    },
    deleteItem(id) {
      this.$confirm("此操作将永久删除该用户, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        deleteRole(id).then((res) => {
          if (res.code == 200) {
            this.search();
          }
          this.$message({
            message: res.ok ? "删除成功" : "删除失败",
            type: res.code == 200 ? "success" : "error",
          });
        });
      });
    },
  },
  created() {
    this.search();
    this.allMenu();
  },
};
</script>
